/**
 *
 */
package com.suieqian.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suieqian.config.Env;
import com.suieqian.dao.SysMenuDao;
import com.suieqian.dto.req.SysMenuReq;
import com.suieqian.dto.resp.SysMenuResp;
import com.suieqian.entity.SysMenu;
import com.suieqian.enums.DelStatusEnum;
import com.suieqian.enums.MenuIdEnum;
import com.suieqian.enums.MenuTypeEnum;
import com.suieqian.exception.ServiceLogicException;
import com.suieqian.service.ISysMenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * 菜单service Impl
 *
 * @author lirch
 */
@Service
//@Transactional
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenu> implements ISysMenuService {


    @Resource
    private SysMenuDao sysMenuDao;

    @Override
    public List<SysMenuResp> getMenus(List<Long> ids, String name) {
        LambdaQueryWrapper<SysMenu> lqw = Wrappers.lambdaQuery();
        lqw.eq(SysMenu::getDelStatus, DelStatusEnum.NORMAL.getCode())
                .in(CollUtil.isNotEmpty(ids), SysMenu::getId, ids)
                .like(StrUtil.isNotBlank(name), SysMenu::getName, name)
                .orderByAsc(SysMenu::getSort);
        List<SysMenu> sysMenus = sysMenuDao.selectList(lqw);
        if (CollUtil.isEmpty(sysMenus)) {
            return null;
        }
        List<SysMenuResp> sysMenuResps = BeanUtil.copyToList(sysMenus, SysMenuResp.class);
        return sysMenuResps;
    }

    @Override
    public List<SysMenuResp> listMenuByPid(Long pid) {
        LambdaQueryWrapper<SysMenu> lqw = Wrappers.lambdaQuery();
        lqw.eq(SysMenu::getDelStatus, DelStatusEnum.NORMAL.getCode())
                .eq(Objects.nonNull(pid), SysMenu::getParentId, pid)
                .orderByAsc(SysMenu::getSort);
        List<SysMenu> sysMenus = sysMenuDao.selectList(lqw);
        if (CollUtil.isEmpty(sysMenus)) {
            return null;
        }
        List<SysMenuResp> sysMenuResps = BeanUtil.copyToList(sysMenus, SysMenuResp.class);
        return sysMenuResps;
    }

    @Override
    public Long createMenu(SysMenuReq reqVO) {
        SysMenu sysMenu = BeanUtil.copyProperties(reqVO, SysMenu.class);
        // 插入数据库
        sysMenu.setCreateTime(LocalDateTime.now())
                .setCreateId(Env.getUser().getUserId())
                .setUpdateId(Env.getUser().getUpdateId())
                .setUpdateName(Env.getUser().getUserAccount())
                .setUpdateTime(LocalDateTime.now())
                .setDelStatus(DelStatusEnum.NORMAL.getCode())
                .setMenuType(MenuTypeEnum.MENU.getType());
        if (Objects.nonNull(reqVO.getParentId()) && MenuIdEnum.ROOT.getId().equals(reqVO.getParentId())) {
            sysMenu.setMenuType(MenuTypeEnum.DIR.getType());
        }
        sysMenuDao.insert(sysMenu);
        // 返回
        return sysMenu.getId();
    }

    @Override
    public void updateMenu(SysMenuReq reqVO) {
        // 校验更新的菜单是否存在
        SysMenu dbMenu = sysMenuDao.selectById(reqVO.getId());
        if (dbMenu == null) {
            throw new ServiceLogicException("菜单ID不存在");
        }
        // 更新到数据库
        SysMenu sysMenu = BeanUtil.copyProperties(reqVO, SysMenu.class);
        // 插入数据库
        sysMenu.setCreateTime(dbMenu.getCreateTime())
                .setUpdateId(Env.getUser().getUpdateId())
                .setUpdateName(Env.getUser().getUserAccount())
                .setUpdateTime(LocalDateTime.now());
        sysMenuDao.updateById(sysMenu);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteMenu(Long menuId) {
        // 标记删除
        SysMenu sysMenu = new SysMenu()
                .setDelStatus(DelStatusEnum.ERROR.getCode()).setId(menuId)
                .setUpdateId(Env.getUser().getUpdateId())
                .setUpdateName(Env.getUser().getUserAccount())
                .setUpdateTime(LocalDateTime.now());
        sysMenuDao.updateById(sysMenu);

        LambdaQueryWrapper<SysMenu> lqw = Wrappers.lambdaQuery();
        lqw.eq(SysMenu::getDelStatus, DelStatusEnum.NORMAL.getCode()).eq(SysMenu::getParentId, menuId);
        sysMenu = new SysMenu().setDelStatus(DelStatusEnum.ERROR.getCode());
        sysMenuDao.update(sysMenu, lqw);

        // TODO 删除授予给角色的权限
//		permissionService.processMenuDeleted(menuId);

    }
}
